/* -*- C++ -*- */
/*===========================================================
 
 .---=||||[ B L O O D Y ]|||||=====================----------....
 |                                                              |
 |    THE  M O S T   B L O O D Y  GAME EVER!                    |
 |                                                              |
 |    bloody harvest is coming... soon,                         |
 |                                              heil Satan!     |
 !                                                              !
 .                                                              ,
  ~~~~-------------================================|||||||||==-~




 Name-Space: {core, logic, editor}
 Module: {draw, coll, scgr, ...}
 Class: <name of class>
 Last modification:  <author>, <time>

 TODO:
 <-------- todo #1 ---------->
 <-------- todo #2 ---------->
 <-------- todo #3 ---------->
        ............
 <-------- todo #n ---------->

 FIX:
 <-------- fix #1 ----------->
 <-------- fix #1 ----------->
         ..........
 <-------- fix #n ----------->




 
 
 ============================================================*/
#ifndef __draw_IndexBuffer_Header_File__
#define __draw_IndexBuffer_Header_File__

#include "common.h"
#include "core/draw/VertexBuffer.h"

namespace core
{
	namespace draw
	{


		/**
		 * Provides buffer for vertex indices. This class is used
		 * for drawing of 3D mesh.
		 */
		class IndexBuffer
		{
		public:

			/**
			 * Creates new index buffer object.
			 * 
			 * @param size Number of indices to be stored in buffer.
			 */
			IndexBuffer(u32 size, GLenum usage = GL_DYNAMIC_COPY_ARB);

			/**
			 * Frees index buffer object and all its data.
			 */
			virtual ~IndexBuffer();

			/**
			 * Gets pointer to inner data to store or update
			 * indices.
			 * 
			 * @param pool Type of storeg (actually ignored)
			 *
			 * @TODO implement proper storeg.
			 */
			u32* lock(GLenum access = GL_WRITE_ONLY_ARB);

			/**
			 * Unlock locked data.
			 */
			void unlock();

			/**
			 * Bind current buffer to OpenGL
			 */
			void bind();

			/**
			 * Unbind current buffer
			 */
			void unbind();

			/**
			 * Draw mesh using indices from this buffer.
			 * 
			 * @param vb Vertex buffer of mesh
			 * @param off Offset in index buffer (count of indices)
			 * @param Count of indices to be used for drawing mesh. 
			 * If count equals to zero, draws whole buffer.
			 */
			void draw(VertexBuffer* vb, GLenum primitiveType, int off = 0, int count = 0);

		private:
			/**
			 * Is index buffer using VBO OpenGL extension.
			 */
			bool usingVbo;
			u32 vboId;
			/**
			 * Pointer to internal data, or null 
			 * if using VBO.
			 */
			u32* data;

			/**
			 * Size of buffer (number of indices stored in buffer).
			 */
			u32 size;

			/**
			 * VBO extension usage flag.
			 */
			GLenum vboUsage;

			/**
			 * VBO extension access flag.
			 */
			GLenum vboAccess;

			/**
			 * Is in Locked state
			 */
			bool locked;

		};

	}
}

#endif //__draw_IndexBuffer_Header_File__
